package com.controller;

import com.controller.vo.ResultVO;
import com.dto.ProductRecommendationDto;
import com.entity.Product;
import com.github.pagehelper.PageInfo;
import com.service.ProductService;
import com.utils.UploadImage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName ProductController
 * @Description 商品信息控制器
 * @Author Kx
 * @Date 2025/5/20 20:01
 * @Version 1.0
 */
// 标识为控制器并且解析JSON数据
@RestController
public class ProductController extends BaseController{

    private final ProductService productService; // 商品业务对象

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * @return com.controller.vo.ResultVO<com.entity.Product>
     * @author: kx
     * @date: 2025/5/20 20:03
     * param userId : 用户编号(可选)
     * @description: 查询所有商品信息
     */
    @GetMapping("/product/findAll")
    public ResultVO<ProductRecommendationDto> findAll(@RequestParam(name = "userId", required = false) Integer userId, HttpServletRequest request) {
        // 查询商品信息
        ProductRecommendationDto productRecommendationDto = productService.findAll(userId, request);
        return success(productRecommendationDto);
    }

    /**
     * @author: kx
     * @date: 2025/5/21 9:51
     * @param page : 当前页
     * @param size : 每页显示数
     * @return com.controller.vo.ResultVO<com.github.pagehelper.PageInfo<com.entity.Product>>
     * @description: 分页查询所有商品
    */
    @GetMapping("/product/findProductsByPage")
    public ResultVO<PageInfo<Product>> findProductsByPage(@RequestParam("page") int page,
                                                          @RequestParam("size") int size) {
        PageInfo<Product> productPageInfo = productService.findProductsByPage(page, size);
        return success(productPageInfo);
    }

    /**
     * @author: kx
     * @date: 2025/6/4 20:28
     * @param title : 商品名称
     * @return com.controller.vo.ResultVO<java.util.List<com.entity.Product>>
     * @description: 根据商品名称模糊查询商品信息
    */
    @GetMapping("/product/searchProductsByTitle")
    public ResultVO<List<Product>> searchProductsByTitle(@RequestParam("title") String title) {
        List<Product> productList = productService.searchProductsByTitle(title);
        return success(productList);
    }

    /**
     * @author: kx
     * @date: 2025/6/4 20:39
     * @param title : 商品名称
     * @param page : 当前页
     * @param size : 每页显示数
     * @return com.controller.vo.ResultVO<com.github.pagehelper.PageInfo<com.entity.Product>>
     * @description: 根据商品名称模糊分页查询商品信息
    */
    @GetMapping("/product/searchProductsByTitleWithPagination")
    public ResultVO<PageInfo<Product>> searchProductsByTitleWithPagination(@RequestParam("title") String title, @RequestParam("page") int page, @RequestParam("size") int size) {
        PageInfo<Product> productPageInfo = productService.searchProductsByTitleWithPagination(title, page, size);
        return success(productPageInfo);
    }

    /**
     * @param title : 商品名称
     * @param brand : 商品品牌
     * @param description : 商品描述
     * @param number : 商品数量
     * @param price  : 商品价格
     * @param image : 商品图片
     * @param categoryId : 商品分类ID
     * @return com.controller.vo.ResultVO
     * @author: kx
     * @date: 2025/5/21 11:16
     * @description: 添加商品信息
     */
    @PostMapping("/product/addProduct")
    public ResultVO<String> addProduct(@RequestParam("title") String title,
                                       @RequestParam("brand") String brand,
                                       @RequestParam("description") String description,
                                       @RequestParam("number") Integer number,
                                       @RequestParam("price") BigDecimal price,
                                       @RequestParam("image") MultipartFile image,
                                       @RequestParam("categoryId") Integer categoryId,
                                       HttpServletRequest request) {

        Product product = new Product();
        product.setTitle(title);
        product.setBrand(brand);
        product.setDescription(description);
        product.setNumber(number);
        product.setPrice(price);
        product.setCategoryId(categoryId);

        // 上传图片
        String imagePath = UploadImage.uploadImage(image, request);

        // 保存图片路径
        product.setMainImage(imagePath);

        productService.addProduct(product, categoryId);

        return success("添加成功");
    }

    /**
     * @author: kx
     * @date: 2025/5/21 20:02
     * @param description : 商品描述
     * @param number : 商品数量
     * @param price : 商品价格
     * @param status : 商品状态
     * @param image : 商品图片(可选)
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @description: 根据编号修改商品信息
    */
    @PostMapping("/product/update")
    public ResultVO<String> updateProduct(@RequestParam(value = "description",required = false) String description,
                                          @RequestParam(value = "number",required = false) Integer number,
                                          @RequestParam(value = "price",required = false) BigDecimal price,
                                          @RequestParam(value = "status",required = false) boolean status,
                                          @RequestParam("productId") Integer productId,
                                          @RequestParam(value = "image",required = false) MultipartFile image,
                                          HttpServletRequest request) {

        Product product = new Product();
        product.setDescription(description);
        product.setNumber(number);
        product.setPrice(price);
        product.setStatus(status);
        product.setProductId(productId);

        // 判断是否需要修改图片
        if (image != null && !image.isEmpty()) {
            // 上传图片并获取图片路径
            String imagePath = UploadImage.uploadImage(image, request);

            product.setMainImage(imagePath);
        }
        productService.updateProduct(product);
        return success("修改商品成功");
    }

    /**
     * @author: kx
     * @date: 2025/6/8 11:33
     * @param brand : 商品品牌
     * @param categoryId : 商品分类ID
     * @return com.controller.vo.ResultVO<java.util.List<com.entity.Product>>
     * @description: 根据商品品牌和商品分类ID查询该品牌下的所有商品(根据销量显示)
    */
    @GetMapping("/product/findHotProductsByBrand")
    public ResultVO<List<Product>> findHotProductsByBrand(@RequestParam("brand") String brand,@RequestParam("categoryId") Integer categoryId) {
        List<Product> hotProductsByBrand = productService.findHotProductsByBrand(brand, categoryId);
        return success(hotProductsByBrand);
    }

    /**
     * @author: kx
     * @date: 2025/6/15 20:42
     * @param categoryId : 商品分类ID
     * @return com.controller.vo.ResultVO<java.util.List<com.entity.Product>>
     * @description: 根据商品分类ID查询该分类下的所有商品
     */
    @GetMapping("/product/getGoodsByCategoryId")
    public ResultVO<List<Product>> getGoodsByCategoryId(@RequestParam("categoryId") Integer categoryId) {
        // 获取查询出来的商品数据
        List<Product> productList = productService.getGoodsByCategoryId(categoryId);
        return success(productList);
    }

    /**
     * 查询热门商品（根据销量排序）
     * @param limit 查询的商品数量
     * @return 热门商品列表
     */
    @GetMapping("/product/findHotProducts")
    public ResultVO<List<Product>> findHotProducts(@RequestParam("limit") int limit) {
        List<Product> hotProducts = productService.findHotProducts(limit);
        return success(hotProducts);
    }
}
